﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Tetris
{
    class Field
    {
        public Frame fr = new Frame();
        public ShapeCollection Sh = new ShapeCollection();
        public int [,] state = new int [40,40];
        public Renderer rend = new Renderer();
        private int marginX = 5;
        private int marginY = 5;
        

        
        public void GameField()
        {
            rend.PrintGameField(5, 5, 20, 25, state);
            for (int i = 0; i < 40; i++)
            {// записываем в массив рамки игрового поля
                state[20, i] = 1;
                //Console.SetCursorPosition(20, i);
                //Console.Write("+");
            }
            for (int i = 0; i < 40; i++)
            {
                state[5, i] = 1;
                //Console.SetCursorPosition(5, i);
                //Console.Write("+");
            }
            for (int i = 0; i < 40; i++)
            {
                state[i, 25] = 1;
                //Console.SetCursorPosition(i, 25);
                //Console.Write("+");
            }
            for (int i = 0; i < 40; i++)
            {
                state[i, 5] = 1;
                //Console.SetCursorPosition(i, 5);
                //Console.Write("+");
            }
        }

        public int ChooseRandomShape()
        {
            Sh.AddShapeToCollection();
            Random rnd = new Random();
            int q = 0;// rnd.Next(0, 7);
            return q;
        }

       /* public void PrintShape()
        {
            int q = ChooseRandomShape();
            Shape shape = Sh.MyShapes[q];               
           initDefoultShapePos(shape);


           int i = 0;
           do
           {               
               if (canMoveDown(shape, i,state))
               {
                   rend.ClearPriviosPosition(shape.b1.x, shape.b1.y,
                                    shape.b2.x, shape.b2.y,
                                    shape.b3.x, shape.b3.y,
                                    shape.b4.x, shape.b4.y);

                   moveShape(shape);
                   rend.RenderShape(shape.b1.x, shape.b1.y,
                                    shape.b2.x, shape.b2.y,
                                    shape.b3.x, shape.b3.y,
                                    shape.b4.x, shape.b4.y);
                   i++;
                   Thread.Sleep(100);
               }
               else 
               {
                   saveShapePosition(shape);
                   PrintShape();
                   if (i == 0) 
                   {                       
                       Console.WriteLine("Game over");
                       break;
                   }

               }
           } while (i != 1000);
           PrintShape();
       }*/

        
        public void initDefoultShapePos(Shape shape)
        {
            shape.b1.x += 11;
            shape.b1.y += 6;

            shape.b2.x += 11;
            shape.b2.y += 6;

            shape.b3.x += 11;
            shape.b3.y += 6;

            shape.b4.x += 11;
            shape.b4.y += 6;               
        }

        public void moveShape(Shape shape)
        {            
            shape.b1.y++;
            shape.b2.y++;            
            shape.b3.y++;            
            shape.b4.y++;
        }

        public void saveShapePosition(Shape shape)
        {
            state[shape.b1.x, shape.b1.y] = 1;
            state[shape.b2.x, shape.b2.y] = 1;
            state[shape.b3.x, shape.b3.y] = 1;
            state[shape.b4.x, shape.b4.y] = 1;
        }
    }
}
